﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Entities;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Filters;
using KaytechSoftware.Cms.App.Common;
using KaytechSoftware.Cms.Services.Dto;
using KaytechSoftware.Cms.Services.Interfaces;
using KaytechSoftware.Cms.Services.Interfaces.Entity;

namespace KaytechSoftware.Cms.App.Areas.Admin.Controllers
{
    public class NewsController : GridController<NewsFiltersModel, NewsModel, NewsDto>
    {
        private readonly INewsService _newsService;
        private readonly IPageService _pageService;

        public NewsController(INewsService newsService, IPageService pageService) : base(newsService)
        {
            _newsService = newsService;
            _pageService = pageService;
        }

        public override ActionResult Add()
        {
            var pageDtos = _pageService.GetAll();
            var model = CreateEntityModel();
            model.RegisterCollection(CollectionKey.Page, pageDtos, p => p.Id, p => p.Title);

            var selectedPageId = Session[SessionKeys.SelectedPage] as long?;
            if (selectedPageId.HasValue)
                model.PageId = selectedPageId.Value;

            return Details(model);
        }

        public override ActionResult Edit(long id)
        {
            var newsDto = _newsService.GetDtoById(id);
            var pageDtos = _pageService.GetAll();

            var model = CreateEntityModel();
            model.FromDto(newsDto);
            model.RegisterCollection(CollectionKey.Page, pageDtos, p => p.Id, p => p.Title);

            return Details(model);
        }

        public override void OnPreSave(NewsModel editModel)
        {
            Session[SessionKeys.SelectedPage] = editModel.PageId;
        }

        public override NewsFiltersModel RestoreOrGetDefaultFilters()
        {
            var filters = new NewsFiltersModel();
            var selectedPageId = Session[SessionKeys.SelectedPage] as long?;
            if (selectedPageId.HasValue)
            {
                filters.SelectedPageId = selectedPageId.Value;
            }
            else
            {
                var pageDtos = _pageService.GetAll();
                if (pageDtos.Any())
                    filters.SelectedPageId = pageDtos.OrderBy(p => p.Id).First().Id;
            }

            RegisterCollectionsForFilters(filters);
            return filters;
        }

        public override void RegisterCollectionsForFilters(NewsFiltersModel filters)
        {
            var pageDtos = _pageService.GetAll();
            filters.RegisterCollection(CollectionKey.Page, pageDtos, p => p.Id, p => p.Title);
            Session[SessionKeys.SelectedPage] = filters.SelectedPageId;
        }

        protected override IList<NewsDto> Filter(NewsFiltersModel filtersModel)
        {
            var items = _newsService.GetAllForPage(filtersModel.SelectedPageId);
            return items;
        }
    }
}
